package com.smsc.headend.task.engine.utils;

import cn.hutool.json.JSONUtil;
import com.smsc.headend.module.protocol.cosem.util.ByteUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * @author wxh
 * @date 2021/6/22 10:30
 */
@Slf4j
public class CustomCosemUtil {
    /**
     * 报文 10000B00 按16进制转换成时间格式给web ["16:00", "11:00"]
     * @param str
     * @return
     */
    public static String formatFriendTimeStr( String str) {
        if (StringUtils.isEmpty(str))  return "";

        if(str.length()!=8) {
            log.debug("time value is invalid" + str);
            return  str;
        }

        //分割成Sunrise Sunset time显示[16:00, 11:00]
        String s = getDivededStrByN(str, 4);
        String[] sArr = s.split(" ");
        StringBuilder sb = new StringBuilder();
        sb.append("[\"").append(formatHexTimeStr(sArr[0])).append("\", \"").append(formatHexTimeStr(sArr[1])).append("\"]");

        return  sb.toString();
    }

    // friend time web那边传 ["16:00", "11:00"]  需要转成十六进制的格式 10000B00
    public static String formatFriendTimeStrToHex(String str) {
        String regex = "\"|\\[|\\]|:|,| ";
        str = str.replaceAll(regex, "");
        String value = getDivededStrByN(str, 2);
        String[] values = value.split(" ");

        StringBuilder buffer = new StringBuilder();
        for (String item:values) {
            String childValue = String.format("%02X", Integer.parseInt(item));
            buffer.append(childValue);
        }

        return buffer.toString();
    }

    // vending接口 Friendly Time 格式 16001100 需要转成 10000B00
    public static String formatVendingFriendTimeStrToHex(String str) {
        str = str.replaceAll(" ", "");
        String value = getDivededStrByN(str, 2);
        String[] values = value.split(" ");

        StringBuilder buffer = new StringBuilder();
        for (String item:values) {
            String childValue = String.format("%02X", Integer.parseInt(item));
            buffer.append(childValue);
        }

        return buffer.toString();
    }


    //每隔n位用空格符分割
    public static String getDivededStrByN(String str, int n) {
        String regex = "(.{"+ n + "})";
        str = str.replaceAll(regex,"$1 ");

        return str;
    }

    /**
     * 报文 0534 按16进制转换为05:52
     * @param s
     * @return
     */
    public static String formatHexTimeStr(String s) {
        String value = getDivededStrByN(s, 2);
        String values[] = value.split(" ");

        StringBuilder buffer = new StringBuilder();
        buffer.append(String.format("%02d", Long.parseLong(values[0], 16)) + ":" + String.format("%02d", Long.parseLong(values[1], 16)));

        return buffer.toString();
    }

    /**
     * 带百分秒 00:00:00 00
     * @param str
     * @return
     */
    public static String parseOctStringTimeWithHundth(String str) {
        byte b[] =  ByteUtils.hexStringToByte(str);
        int offset = 0;

        StringBuffer sb = new StringBuffer();

        if (b[offset] >= 0 && b[offset] <= 23) {
            sb.append(String.format("%02d", b[offset]));
        } else {
            sb.append(String.format("%02X", b[offset]));
        }
        sb.append(":");
        offset += 1;

        if (b[offset] >= 0 && b[offset] <= 59) {
            sb.append(String.format("%02d", b[offset]));
        } else {
            sb.append(String.format("%02X", b[offset]));
        }
        sb.append(":");
        offset += 1;

        if (b[offset] >= 0 && b[offset] <= 59) {
            sb.append(String.format("%02d", b[offset]));
        } else {
            sb.append(String.format("%02X", b[offset]));
        }

        sb.append(" ");
        offset += 1;
        if (b[offset] >= 0 && b[offset] <= 99) {
            sb.append(String.format("%02d", b[offset]));
        } else {
            sb.append(String.format("%02X", b[offset]));
        }
        return sb.toString();
    }


    /**
     * 带空格的week FFFF-FF-01 FF
     * @param str
     * @return
     */
    public static String parseOctStringDateWithSPWeek(String str) {
        byte b[] = new byte[5];
        ByteUtils.numberStringToBytes(str, 16, b, 0);
        int offset = 0;
        int year = ((b[offset++]<<8)& 0xff00) + (b[offset++]& 0xff);
        int month = b[offset++] & 0xff;
        int day = b[offset++] & 0xff;
        int dayOfWeek = b[offset++] & 0xff;

        String timeValue = String.format("%04d-%02d-%02d %02d", year, month, day, dayOfWeek);

        timeValue = timeValue.replaceAll("65535", "FFFF");
        timeValue = timeValue.replaceAll("255", "FF");
        timeValue = timeValue.replaceAll("254", "FE");
        timeValue = timeValue.replaceAll("253", "FD");

        return timeValue;
    }


//    public static void main(String[] args) {
//        log.info("formatFriendTimeStr {}", formatFriendTimeStr("10000B00"));
//        log.info("formatFriendTimeStrToHex {}", formatFriendTimeStrToHex("[\"16:00\", \"11:00\"]"));
//    }
}
